package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.HoaTietDao;
import com.tt.fashionWebSite.domain.HoaTiet;
import com.tt.fashionWebSite.domain.MauSac;
import com.tt.fashionWebSite.dto.HoaTietDto;
import com.tt.fashionWebSite.dto.JsonResponseCode;
import com.tt.fashionWebSite.dto.MauSacDto;
import com.tt.fashionWebSite.service.Ad_ProductMotifService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_productMotifService")
public class Ad_ProductMotifServiceImp extends GenericService implements Ad_ProductMotifService {
    @Autowired
    @Qualifier("hoaTiet")
    private HoaTietDao hoaTietDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductMotif(HoaTietDto productMotifDto) {
        HoaTiet hoaTiet = hoaTietDao.findById(productMotifDto.getMaHoaTiet());
        if(hoaTiet == null){
            hoaTiet = new HoaTiet();
            hoaTiet.setMaHoaTiet(productMotifDto.getMaHoaTiet());
            hoaTiet.setTenHoaTiet(productMotifDto.getTenHoaTiet());
            hoaTietDao.insert(hoaTiet);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<HoaTietDto> getAllMotif () {
        return mapper.mapCollection(hoaTietDao.getAllMotif(), HoaTietDto.class);
    }

    public HoaTietDto getMotifInfoEdit(HoaTietDto motifDto){
        HoaTiet hoaTiet = hoaTietDao.findById(motifDto.getMaHoaTiet());
        HoaTietDto hoaTietDto = mapper.map(hoaTiet, HoaTietDto.class,HOA_TIET_MAP_ID);
        return  hoaTietDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public HoaTietDto registerMotifEdit(HoaTietDto motifDto) {
        HoaTiet hoaTiet = hoaTietDao.findById(motifDto.getMaHoaTiet());
        try {
            hoaTiet.setMaHoaTiet(motifDto.getMaHoaTiet());
            hoaTiet.setTenHoaTiet(motifDto.getTenHoaTiet());
            hoaTietDao.update(hoaTiet);
            return motifDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductMotifDelete(List<HoaTietDto> productMotif) {
        for(int i = 0; i < productMotif.size(); i++) {
            String id = productMotif.get(i).getMaHoaTiet();
            hoaTietDao.delete(id);
        }
        return JsonResponseCode.SUCCESS;
    }
}

